// pages/business/create-live/create-live.js
var upload = require('../../../utils/upload.js');
const util = require('../../../utils/util.js');
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    startDate:'日期',
    startTime:'时间',
    endDate:"日期",
    endTime:"时间",
    cover: "",
    bg:'',
    getSelect:[],
    bg_id:"",
    cover_id:"",
  },
  // 时间日期选择器
  bindDateChange(e){
    let type = e.currentTarget.dataset.type,value = e.detail.value;
    this.setData({
      [type]:value
    })
  },
  // 选择直播商品
  selectGoods(){
    wx.navigateTo({
      url: "/pages/business/live-goods/live-goods"
    })
  },
  // input
  blurChange(e){
    let type = e.currentTarget.dataset.type,value = e.detail.value;
    this.setData({
      [type]:value
    })
  },  
  // 将图片转为media
  mediaChange(url,callback){
    wx.request({
      url: app.globalData.urls + '/live-product/wechat-media',
      header: app.getRequestHeader(),
      method:'POST',
      data: {
        url:url
      },
      success: function (res) {
        if (res.data.code == 200) {
          let getdata =  res.data.data;
          console.log("---------",getdata)
          callback(getdata.media_id);
        }
      }
    })
  },
  // 上传图片
  upShopLogo: function () {
    var that=this;
    upload.upShopLogo(res=>{
      this.mediaChange(res.data.url,(id)=>{
        that.setData({
          cover_id: id
        })
      });
      this.setData({
        cover: res.data.url
      })
    })
  },
  // 上传背景图片
  upShopbg: function () {
    var that=this;
    upload.upShopLogo(res=>{
      this.mediaChange(res.data.url,id=>{
        console.log("-----------res",id)
        that.setData({
          bg_id: id
        })
      });

      this.setData({
        bg: res.data.url
      })
    })
  },
  // 删除上传封面
  delCoverimg(){
    this.setData({
      cover: ""
    })
  },
  // 删除背景封面
  delBgimg(){
    this.setData({
      bg: ""
    })
  },
  // 点击申请
  confrim(){
    if(this.data.getSelect.length==0){
      wx.showToast({
        icon:"none",
        title: '请选择直播商品',
      })
      return;
    }
    this.setData({
      isShow:true
    })
    let that=this;
    wx.request({
      url: app.globalData.urls + '/live/create-room',
      header: app.getRequestHeader(),
      method:'POST',
      data: {
        bdmin_user_id:util.getBdmin_user_id(),
        name:this.data.name,
        shareImg:this.data.cover_id,
        startTime:this.data.startDate+" "+this.data.startTime,
        endTime:this.data.endDate+" "+this.data.endTime,
        anchorName:this.data.anchorName,
        anchorWechat:this.data.anchorWechat,
        coverImg:this.data.bg_id,
        productIds:this.data.getSelect,
      },
      success: function (res) {
        that.setData({
          isShow:false
        })
        if (res.data.code == 200) {
          let getdata =  res.data.data;
          that.setData({
            name:'',
            anchorName:"",
            anchorWechat:''
          })
          wx.showModal({
            title: '创建成功',
            content:'请到【小程序直播】小程序开启您的直播之旅吧~',
            showCancel:false,
            complete:function(){
              wx.navigateTo({
                url: '/pages/business/live-lists/live-lists',
              })
            }
          })
          wx.removeStorageSync('liveGoods')
        }else{
          wx.showToast({
            icon:"none",
            title: res.data.message.errmsg || res.data.message,
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.isLive();
    wx.showModal({
      title: '官方提示',
      content:'直播间内不可发表不良言论，请遵守国家相关法律规定，情节严重永久封号',
      showCancel:false
      
    })
  },
  // 是否可以开直播
  isLive(){
    let that=this;
    wx.request({
      url: app.globalData.urls + '/live/check-add',
      header: app.getRequestHeader(),
      method:'POST',
      data: {
        bdmin_user_id:util.getBdmin_user_id()
      },
      success: function (res) {
        that.setData({
          isShow:false
        })
        if (res.data.code == 200) {
          
        }else{
          wx.showModal({
            title: res.data.message.errmsg || res.data.message,
            content:'',
            showCancel:false,
            complete:function(){
              wx.navigateBack({
                delta: -1,
              })
            }
          })
        }
      }
    })
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let getSelect = wx.getStorageSync('liveGoods')
    this.setData({
      getSelect:getSelect,
    })
  },
  onHide(){
    
  },
})